package kr.hkit.summary_java.serivceUI;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;

import java.awt.Dimension;
import java.awt.GridLayout;

import javax.swing.JLabel;

import java.awt.BorderLayout;

import javax.swing.JButton;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.sql.Connection;
import java.sql.SQLException;

import kr.hkit.myframework.db.ConnectionFactory;
import kr.hkit.myframework.uicomp.InputPanel;
import kr.hkit.summary_java.dao.DaoInterface;
import kr.hkit.summary_java.dao.DepartmentDAO;
import kr.hkit.summary_java.erp.MainFrame;
import kr.hkit.summary_java.erp.dto.Department;

public class PanelDepartmentUI extends JPanel implements ActionListener{
	private InputPanel pDeptno;
	private InputPanel pDeptName;
	private InputPanel pFloor;
	
	private DepartmentDAO dao_dept;
	
	private JButton btnAdd;
	private JButton btnClose;
	
	private JPanel panel_Center;
	private JPanel panel_Bottom;
	
	private String btnText;
	
	
	public String getBtnAdd() {
		return btnAdd.getText();
	}

	public void setBtnAdd(String text) {
		btnAdd.setText(text);
	}


	/**
	 * Create the panel.
	 */
	public PanelDepartmentUI(String btnText, DepartmentDAO dao_dept) {
		this.btnText = btnText;
		this.dao_dept = dao_dept;
		initialize();
		
	}

	private void initialize() {
		setPreferredSize(new Dimension(240, 160));
		setLayout(new BorderLayout(0, 0));
		
		panel_Center = new JPanel();
		add(panel_Center);
		panel_Center.setLayout(new GridLayout(0, 1, 0, 0));
		
		pDeptno = new InputPanel("부서번호");
		panel_Center.add(pDeptno);
		pDeptno.getTf().addFocusListener(new FocusAdapter() {

			@Override
			public void focusLost(FocusEvent e) {
				super.focusLost(e);
				
				int dno = Integer.parseInt(pDeptno.getTf().getText().trim());
				Department d = new Department();
				d.setDeptNo(dno);
				
				try {
					d = dao_dept.searchItem(d);
					pDeptName.setTf(d.getDeptName());
					pFloor.setTf(d.getFloor()+""); 

					if ((btnAdd.getText().equals("삭제")) || (btnAdd.getText().equals("검색"))) {
						panelDisable();
						} else if (btnAdd.getText().equals("수정")) {
							panelEnable1();
						}
					
				} catch (SQLException e1) {
					e1.printStackTrace();
				} catch (NullPointerException e1) {
					if (!btnAdd.getText().equals("추가")) {
						JOptionPane.showMessageDialog(PanelDepartmentUI.this, "해당 부서가 없습니다.");
					} else {
						JOptionPane.showMessageDialog(PanelDepartmentUI.this, "추가 가능합니다.");
					}
				}
			}
		});
		
		pDeptName = new InputPanel("부서명");
		panel_Center.add(pDeptName);
		
		pFloor = new InputPanel("위치");
		panel_Center.add(pFloor);
		
		panel_Bottom = new JPanel();
		add(panel_Bottom, BorderLayout.SOUTH);
		
		
		btnAdd = new JButton(btnText);
		btnAdd.addActionListener(this);
		panel_Bottom.add(btnAdd);
		

		if ((btnAdd.getText().equals("삭제")) || (btnAdd.getText().equals("검색"))) {
			System.out.println("출력확인용");
			panelDisable();
		}
		
		btnClose = new JButton("닫기");
		btnClose.addActionListener(this);
		panel_Bottom.add(btnClose);
		
	}
	
	
	private Department getDeptAll() {
		int dno = Integer.parseInt(pDeptno.getTf().getText().trim());
		String name = pDeptName.getTf().getText();
		int floor = Integer.parseInt(pFloor.getTf().getText());
		
		return new Department(dno, name, floor);
	} 

	private Department getDept() {
		int dno = Integer.parseInt(pDeptno.getTf().getText().trim());
		
		return new Department(dno);
	}
	
	private void panelDisable() {
		pDeptno.getTf().setEnabled(true); 
		pDeptName.getTf().setEnabled(false);
		pFloor.getTf().setEnabled(false);
	}
	 
	private void panelEnable1() { 
		pDeptno.getTf().setEnabled(false);
		pDeptName.getTf().setEnabled(true);
		pFloor.getTf().setEnabled(true);
	}
	
	private void panelEnable() { 
		pDeptno.getTf().setEnabled(true);
		pDeptName.getTf().setEnabled(true);
		pFloor.getTf().setEnabled(true);
	}
	
	private void panelClear() { 
		pDeptno.getTf().setText("");
		pDeptName.getTf().setText("");
		pFloor.getTf().setText("");
	}
	
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == btnClose) { //닫기
			actionPerformedBtnClose(e);
		} else if (e.getActionCommand().equals("추가")){
			actionPerformedBtnAdd(e);
		} else if(e.getActionCommand().equals("삭제")){
			actionPerformedBtnDelete(e);
		} else if(e.getActionCommand().equals("수정")){
			actionPerformedBtnUpdate(e);
		} else if(e.getActionCommand().equals("검색")){
			actionPerformedBtnSearch(e);
		} 
	}
	

	private void actionPerformedBtnSearch(ActionEvent e) { //검색
		
		Department d = getDept();
		
		try {
			JOptionPane.showMessageDialog(this, dao_dept.searchItem(d));
			panelClear();
		} catch (SQLException e1) {
			JOptionPane.showMessageDialog(this, "검색 실패! 다시 확인하세요.");
		} 
	}

	private void actionPerformedBtnUpdate(ActionEvent e) { //수정
		
		Department d = getDeptAll();
		JOptionPane.showMessageDialog(this, d);
		
		try {
			dao_dept.updateItemByNo(d);
			JOptionPane.showMessageDialog(this, "수정되었습니다.");
			panelClear();
			panelEnable();
		} catch (SQLException e1) {
			JOptionPane.showMessageDialog(this, "수정 실패! 다시 확인하세요.");
			
		} 
	}

	
	protected void actionPerformedBtnDelete(ActionEvent e) { //삭제

		Department d = getDept();
		
		JOptionPane.showMessageDialog(this, d);
		
		try {
			dao_dept.deleteItemByNo(d);
			JOptionPane.showMessageDialog(this, "삭제되었습니다.");
			panelClear();
		} catch (SQLException e1) {
			JOptionPane.showMessageDialog(this, "삭제 실패! 다시 확인하세요.");
		} 
	}
	
	
	protected void actionPerformedBtnAdd(ActionEvent e) { //추가
	
		Department d = getDeptAll();
		
		JOptionPane.showMessageDialog(this, d);
		
		try {
			dao_dept.insertItem(d);
			JOptionPane.showMessageDialog(this, "입력되었습니다.");
			panelClear();
		} catch (SQLException e1) {
			JOptionPane.showMessageDialog(this, "입력 실패! 다시 확인하세요.");
			
		} 
	}

	
	protected void actionPerformedBtnClose(ActionEvent e) { //닫기
	
		panel_Center.setVisible(false);
		panel_Bottom.setVisible(false);
	
	}
}
